package com.sq.partner.manager.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.dao.IGatewayAppDao;
import com.sq.partner.manager.dao.IGatewayAppTranstypeDao;
import com.sq.partner.manager.dao.IGatewayChannelInfoConfigDao;
import com.sq.partner.manager.mybatis.model.GatewayApp;
import com.sq.partner.manager.mybatis.model.GatewayAppTranstype;
import com.sq.partner.manager.mybatis.model.GatewayChannelInfo;
import com.sq.partner.manager.mybatis.model.TreeDto;
import com.sq.partner.manager.service.IGatewayAppService;
import com.sq.partner.manager.service.IGatewayClearCacheManager;
import com.sq.partner.manager.util.Page;

@Service
public class GatewayAppServiceImpl implements IGatewayAppService {

	@Resource
	private IGatewayAppDao appDao;
	@Resource
	private IGatewayAppTranstypeDao appTranstypeDao;
	@Resource
	private IGatewayChannelInfoConfigDao channelInfoConfigDao;
	@Resource
	IGatewayClearCacheManager gatewayClearCacheManager;
	@Override
	public List<GatewayApp> queryAllAppList() throws Exception {
		List<GatewayApp> appList = appDao.queryList();
		return appList;
	}

	@Override
	public void saveOrUpdateApp(String flag, GatewayApp app) throws Exception {
		if(flag.equals("update")){
			appDao.update(app);
		}else if(flag.equals("save")){
			appDao.save(app);
		}
		gatewayClearCacheManager.clearAllCache(app.getAppid());
	}

	@Override
	public List<GatewayApp> queryList(GatewayApp app) throws Exception {
		List<GatewayApp> appList = appDao.queryList(app);
		return appList;
	}

	@Override
	public Page queryPage(Page page, String appId) throws Exception{
		Page appPage = appDao.queryPage(page, appId);
		return appPage;
	}

	@Override
	public List<GatewayAppTranstype> queryAppTranstypeByFkAppId(
			GatewayAppTranstype appTranstype) throws Exception {
		List<GatewayAppTranstype> appTranstypeList = appTranstypeDao.queryByFkAppId(appTranstype);
		return appTranstypeList;
	}

	@Override
	public Result delApp(String appId) throws Exception {
		//现根据appId查询是否存在与之关联的channelInfo
		GatewayChannelInfo channelInfo = new GatewayChannelInfo();
		channelInfo.setFkAppid(appId);
		List<GatewayChannelInfo> channelInfoList = channelInfoConfigDao.getValidListByAppid(channelInfo);
		if(channelInfoList.size() != 0){
			return Result.error("删除失败，请先删除关联数据");
		}else{
			appTranstypeDao.delByAppId(appId);//先删除该app所关联的appTranstype
			appDao.delByAppId(appId);
			return Result.success("删除成功");
		}
	}

	@Override
	public GatewayApp queryAppByAppId(String appId) throws Exception {
		GatewayApp app = new GatewayApp();
		app.setAppid(appId);
		GatewayApp appFind = appDao.queryByApp(app);
		return appFind;
	}

	@Override
	public Result delAppTranstypeByAppTranstypeId(Long appTranstypeId)
			throws Exception {
		Result result = appTranstypeDao.delByAppTranstype(appTranstypeId);
		return result;
	}

	@Override
	public GatewayAppTranstype checkTransCodeIfUsedInOneApp(String appId,
			String transCode) throws Exception {
		GatewayAppTranstype appTranstype = appTranstypeDao.checkTransCodeIfUsedInOneApp(appId, transCode);
		return appTranstype;
	}

	@Override
	public void saveAppTranstype(GatewayAppTranstype appTranstype)
			throws Exception {
		appTranstypeDao.save(appTranstype);
	}
	
	@Override
	public List<TreeDto> querySubChannelTreeByParentChannelCode(
							String parentChannelCode) throws Exception{
		return appDao.querySubChannelTreeByParentChannelCode(parentChannelCode);
	}

	@Override
	public List<GatewayApp> loadApplicationData() throws Exception {
		
		return appDao.loadApplicationData();
	}

}
